<?php

namespace app\api\services\admin\setting;

use app\api\common\service\BaseServices;
use app\api\common\traits\ServicesTrait;
use app\api\dao\admin\setting\AccountDao;
use think\db\Where;
use think\Exception;
use think\facade\Config;

class AccountServices extends BaseServices
{
    use ServicesTrait;

    public function __construct(AccountDao $dao)
    {
        $this->dao = $dao;
    }

    public function getInfo($where, $field = '*'): array
    {
        $list = $this->dao->get($where)->toArray();
        unset($list['pwd']);
        unset($list['salt']);
        return compact('list');
    }

    /**
     * 修改密码
     * @param $where
     * @param $data
     *
     * @return mixed
     * @throws \think\Exception
     */
    public function password($where, $data): mixed
    {
        $list        = $this->dao->get($where);
        $salt        = $list['salt'] ?? '';
        $oldPassword = md5($salt . $data['old_password']);
        $newPassword = md5($salt . $data['password']);
        if ($oldPassword !== $list['pwd']) {
            throw new Exception('原始密码错误');
        }
        if ($data['password_confirm'] !== $data['password']) {
            throw new Exception('两次密码输入不一致');
        }
        $data = ['pwd' => $newPassword];
        return $this->dao->update($where, $data);
    }

}
